package com.yx.cloud.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.adapter.FurnitureImageListAdapter;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.bean.FurnitureImageBean;
import com.yx.cloud.databinding.FurnitureEditBinding;
import com.yx.cloud.map.YXMapView;
import com.yx.cloud.utils.RomUtils;
import com.iot.common.utils.Event;
import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class FurnitureEditActivity extends BaseActivity implements FurnitureImageListAdapter.InnerItemOnclickListener ,View.OnClickListener{

    private static String TAG = "FurnitureEditActivity";
    private List<FurnitureImageBean> furnitureImageBeans = new ArrayList<>();
    RecyclerView recyclerView;
    LinearLayout llFurnitureSize;
    ImageView ivFurnitureImage;
    TextView tvFurnitureName;
    ImageView ivReturn;
    RadioButton rBFurnitureMini;
    RadioButton rBFurnitureSmall;
    RadioButton rBFurnitureLager;
    RadioButton rBFurnitureMax;
    TextView tvMini,tvSmall,tvLarge,tvMax;
    FurnitureImageBean furnitureImageBean;
    private YXMapView yxMapView;
    private FurnitureEditBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onCreate(savedInstanceState);
        }
        binding = FurnitureEditBinding.inflate(getLayoutInflater());
        setContentView(R.layout.furniture_edit);
        EventBus.getDefault().register(this);
        initView();
        initMapView();
    }

    private void initView() {
        initData();
        ivFurnitureImage = findViewById(R.id.iv_furniture_image);
        llFurnitureSize = findViewById(R.id.ll_furniture_size);
        tvFurnitureName = findViewById(R.id.tv_furniture_name);
        ivReturn = findViewById(R.id.iv_furniture_return);
        rBFurnitureMini = findViewById(R.id.funiture_mini);
        rBFurnitureSmall = findViewById(R.id.funiture_small);
        rBFurnitureLager = findViewById(R.id.funiture_large);
        rBFurnitureMax = findViewById(R.id.funiture_max);
        tvMini = findViewById(R.id.tv_mini);
        tvSmall = findViewById(R.id.tv_small);
        tvLarge = findViewById(R.id.tv_large);
        tvMax = findViewById(R.id.tv_max);
        //创建适配器，在适配器中导入数据 1.当前类 2.list_view一行的布局 3.数据集合
        FurnitureImageListAdapter furnitureImageListAdapter = new FurnitureImageListAdapter(FurnitureEditActivity.this, furnitureImageBeans);
        recyclerView = findViewById(R.id.rv_furniture_recycler); //将适配器导入Listview
        //设置线性布局管理器类型
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        recyclerView.setAdapter(furnitureImageListAdapter);
        furnitureImageListAdapter.setOnInnerItemOnClickListener(this);
        ivReturn.setOnClickListener(this);
        rBFurnitureMini.setOnClickListener(this);
        rBFurnitureSmall.setOnClickListener(this);
        rBFurnitureLager.setOnClickListener(this);
        rBFurnitureMax.setOnClickListener(this);
    }

    private void initData() {
        float[][] singleBedSize = {{0.9f, 1.9f}, {1, 2}, {1.2f, 2}, {1.5f, 2}};
        FurnitureImageBean singleBed = new FurnitureImageBean(getString(R.string.Single_bed), R.mipmap.single_bed, singleBedSize);
        furnitureImageBeans.add(singleBed);
        float[][] doubleBedSize = {{2, 1.35f}, {2, 1.5f}, {2, 1.8f}, {2.3f, 1.8f}};
        FurnitureImageBean doubleBed = new FurnitureImageBean(getString(R.string.Double_bed), R.mipmap.double_bed, doubleBedSize);
        furnitureImageBeans.add(doubleBed);
        float[][] singleSofaSize = {{0.64f, 0.63f}, {0.8f, 0.7f}, {0.95f, 0.9f}, {1f, 0.9f}};
        FurnitureImageBean singleSofa = new FurnitureImageBean(getString(R.string.Single_sofa), R.mipmap.single_sofa, singleSofaSize);
        furnitureImageBeans.add(singleSofa);
        float[][] doubleSofaSize = {{1.2f, 0.6f}, {1.4f, 0.62f}, {1.26f, 0.8f}, {1.5f, 0.9f}};
        FurnitureImageBean doubleSofa = new FurnitureImageBean(getString(R.string.Double_sofa), R.mipmap.double_sofa, doubleSofaSize);
        furnitureImageBeans.add(doubleSofa);
        float[][] threeSeetSize = {{1.75f, 0.8f}, {2.34f, 0.75f}, {2.15f, 0.85f}, {1.91f, 0.88f}};
        FurnitureImageBean threeSeetSofa = new FurnitureImageBean(getString(R.string.Three_seet_sofa), R.mipmap.three_seat_sofa, threeSeetSize);
        furnitureImageBeans.add(threeSeetSofa);
        float[][] lShapedSofaSize = {{2.7f, 1.45f}, {2.81f, 1.68f}, {2.34f, 1.81f}, {4.05f, 1.85f}};
        FurnitureImageBean lShapedSofa = new FurnitureImageBean(getString(R.string.L_shaped_sofa), R.mipmap.l_shaped_sofa, lShapedSofaSize);
        furnitureImageBeans.add(lShapedSofa);
        float[][] tvCabinetSize = {{1.4f, 0.35f}, {1.6f, 0.35f}, {2f, 0.3f}, {2.2f, 0.3f}};
        FurnitureImageBean tvCabinet = new FurnitureImageBean(getString(R.string.Tv_cabinet), R.mipmap.tv_cabinet, tvCabinetSize);
        furnitureImageBeans.add(tvCabinet);
        float[][] diningTableChairSize = {{0.8f, 0.8f}, {0.76f, 1.2f}, {0.8f, 1.5f}, {1.2f, 1.8f}};
        FurnitureImageBean diningTableChair = new FurnitureImageBean(getString(R.string.Dining_table_chair), R.mipmap.dining_table_chair, diningTableChairSize);
        furnitureImageBeans.add(diningTableChair);
        float[][] bedsideCupboardSize = {{0.45f, 0.36f}, {0.5f, 0.4f}, {0.61f, 0.44f}, {0.78f, 0.44f}};
        FurnitureImageBean bedsideCupboard = new FurnitureImageBean(getString(R.string.Bedside_cupboard), R.mipmap.bedside_cupboard, bedsideCupboardSize);
        furnitureImageBeans.add(bedsideCupboard);
        float[][] wardrobeSize = {{1.4f, 0.59f}, {1.93f, 0.58f}, {2.2f, 0.66f}, {2.9f, 0.55f}};
        FurnitureImageBean wardrobe = new FurnitureImageBean(getString(R.string.Wardrobe), R.mipmap.wardrobe, wardrobeSize);
        furnitureImageBeans.add(wardrobe);
        float[][] shoeCabinetSize = {{0.62f, 0.32f}, {0.85f, 0.38f}, {0.93f, 0.42f}, {1.1f, 0.39f}};
        FurnitureImageBean shoeCabinet = new FurnitureImageBean(getString(R.string.Shoe_cabinet), R.mipmap.shoe_cabinet, shoeCabinetSize);
        furnitureImageBeans.add(shoeCabinet);
        float[][] deskSize = {{0.7f, 0.5f}, {0.6f, 0.9f}, {0.65f, 1.1f}, {0.7f, 1.3f}};
        FurnitureImageBean desk = new FurnitureImageBean(getString(R.string.Desk), R.mipmap.desk, deskSize);
        furnitureImageBeans.add(desk);
        float[][] teaTableSize = {{0.8f, 0.4f}, {0.6f, 0.6f}, {1.4f, 0.7f}, {1, 1}};
        FurnitureImageBean teaTable = new FurnitureImageBean(getString(R.string.Tea_table), R.mipmap.tea_table, teaTableSize);
        furnitureImageBeans.add(teaTable);
        float[][] carpetSize = {{0.7f, 1.6f}, {1.4f, 2}, {1.6f, 2.3f}, {2, 2.5f}};
        FurnitureImageBean carpet = new FurnitureImageBean(getString(R.string.Carpet), R.mipmap.carpet, carpetSize);
        furnitureImageBeans.add(carpet);
        float[][] refrigeratorSize = {{0.58f, 0.5f}, {0.55f, 0.53f}, {0.65f, 0.90f}, {0.67f, 0.92f}};
        FurnitureImageBean refrigerator = new FurnitureImageBean(getString(R.string.Refrigerator), R.mipmap.refrigerator, refrigeratorSize);
        furnitureImageBeans.add(refrigerator);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //MapViewManager.autoAreaEditBack();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
        }
    }

    private void initMapView() {
        yxMapView = findViewById(R.id.furniture_2d_map);
        yxMapView.initMap(AppContext.get().getCurrentDevice().getMap());
    }

    @Override
    public void itemClick(View v, int position) {
        switch (v.getId()) {
            case R.id.iv_furniture_image:
                furnitureImageBean = furnitureImageBeans.get(position);
                showFurnitureSize(furnitureImageBean.getImageId(),furnitureImageBean.getName());
                //mapViewFragment.addFurnitureScaleView(position,1);
                binding.tvMini.setText(furnitureImageBean.getSize()[0][0] + "m*"  + furnitureImageBean.getSize()[0][1] + "m");
                break;
        }
    }


    private void showFurnitureSize(int furnitureId,String name){
        llFurnitureSize.setVisibility(View.VISIBLE);
        tvFurnitureName.setText(name);
        ivFurnitureImage.setImageResource(furnitureId);
        hideFurnitureImage();
    }

    private void showFurnitureImage(){
        recyclerView.setVisibility(View.VISIBLE);
        hideFurnitureSize();
    }

    private void hideFurnitureSize(){
        binding.llFurnitureSize.setVisibility(View.INVISIBLE);
    }

    private void hideFurnitureImage(){
        recyclerView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View view) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (view.getId()){
            case R.id.iv_furniture_return:
                finish();
                break;
            case R.id.iv_furniture_save:
               /* VirtualWallManager.getInstance(this).setViews(mapViewFragment.getFurnitureScaleViews());
                VirtualWallManager.getInstance(this).saveViewToLocal();
                ToastUtil.showShort(this, R.string.Furniture_save);*/
                break;
            case R.id.funiture_mini:
                rBFurnitureMini.setChecked(true);
                rBFurnitureSmall.setChecked(false);
                rBFurnitureLager.setChecked(false);
                rBFurnitureMax.setChecked(false);
                tvMini.setText(furnitureImageBean.getSize()[0][0] + "m*" + furnitureImageBean.getSize()[0][1] + "m");
                tvMini.setVisibility(View.VISIBLE);
                tvSmall.setVisibility(View.INVISIBLE);
                tvLarge.setVisibility(View.INVISIBLE);
                tvMax.setVisibility(View.INVISIBLE);
                //mapViewFragment.changeFurnitureScaleView(1);
                break;
            case R.id.funiture_small:
                rBFurnitureMini.setChecked(false);
                rBFurnitureSmall.setChecked(true);
                rBFurnitureLager.setChecked(false);
                rBFurnitureMax.setChecked(false);
                tvSmall.setText(furnitureImageBean.getSize()[1][0] + "m*" + furnitureImageBean.getSize()[1][1] + "m");
                tvSmall.setVisibility(View.VISIBLE);
                tvMini.setVisibility(View.INVISIBLE);
                tvLarge.setVisibility(View.INVISIBLE);
                tvMax.setVisibility(View.INVISIBLE);
                //mapViewFragment.changeFurnitureScaleView(2);
                break;
            case R.id.funiture_large:
                rBFurnitureMini.setChecked(false);
                rBFurnitureSmall.setChecked(false);
                rBFurnitureLager.setChecked(true);
                rBFurnitureMax.setChecked(false);
                tvLarge.setText(furnitureImageBean.getSize()[2][0] + "m*" + furnitureImageBean.getSize()[2][1] + "m");
                tvLarge.setVisibility(View.VISIBLE);
                tvSmall.setVisibility(View.INVISIBLE);
                tvMini.setVisibility(View.INVISIBLE);
                tvMax.setVisibility(View.INVISIBLE);
                //mapViewFragment.changeFurnitureScaleView(3);
                break;
            case R.id.funiture_max:
                rBFurnitureMini.setChecked(false);
                rBFurnitureSmall.setChecked(false);
                rBFurnitureLager.setChecked(false);
                rBFurnitureMax.setChecked(true);
                tvMax.setText(furnitureImageBean.getSize()[3][0] + "m*" + furnitureImageBean.getSize()[3][1] + "m");
                tvMax.setVisibility(View.VISIBLE);
                tvSmall.setVisibility(View.INVISIBLE);
                tvLarge.setVisibility(View.INVISIBLE);
                tvMini.setVisibility(View.INVISIBLE);
                //mapViewFragment.changeFurnitureScaleView(4);
                break;
        }
    }
}
